Conditions | 3 |
Paths | 4 |
Total Lines | 127 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
17 | function Slf4j (name, context) { |
||
18 | if (!context) { |
||
19 | context = DefaultContext |
||
20 | } |
||
21 | // backward compatibility for 0.1.x/0.2.x |
||
22 | if (!(context instanceof Context)) { |
||
23 | context = new Slf4j.Context(context, arguments[2]) |
||
24 | } |
||
25 | var mdc = {} |
||
26 | var self = this |
||
27 | |||
28 | function render (pattern, substitutions) { |
||
29 | var chunks = pattern.split('{}') |
||
30 | return substitutions.reduce(function (carrier, entry) { |
||
31 | if (chunks.length > 0) { |
||
32 | return carrier + Renderer.any(entry, false) + chunks.shift() |
||
33 | } else { |
||
34 | return carrier + '\r\n' + Renderer.any(entry, true) |
||
35 | } |
||
36 | }, chunks.shift()) |
||
37 | } |
||
38 | |||
39 | function log (level, pattern) { |
||
40 | level = Level.find(level) |
||
41 | if (level.weight < context.getLevel(name).weight) { |
||
42 | return |
||
43 | } |
||
44 | var prefix = '[' + level.id.toUpperCase() + ']' |
||
45 | if (name) { |
||
46 | prefix += ' ' + name |
||
47 | } |
||
48 | var mdcPrefix = Object.keys(mdc).map(function (key) { |
||
49 | return key + '=' + Renderer.any(mdc[key]) |
||
50 | }).join(', ') |
||
51 | if (mdcPrefix) { |
||
52 | prefix += ' [' + mdcPrefix + ']' |
||
53 | } |
||
54 | var subs = [].slice.call(arguments, 2) |
||
55 | var writer = context.getWriter(name) || Logger |
||
56 | writer.write(render(prefix + ': ' + pattern, subs)) |
||
57 | } |
||
58 | |||
59 | // noinspection JSUnusedGlobalSymbols |
||
60 | this.log = log |
||
61 | |||
62 | Level.explicit.forEach(function (level) { |
||
63 | self[level.id.toLowerCase()] = function () { |
||
64 | log.apply(null, [level].concat([].slice.call(arguments))) |
||
65 | } |
||
66 | }) |
||
67 | |||
68 | this.getName = function () { return name } |
||
69 | |||
70 | this.setWriter = function (writer) { |
||
71 | context.setWriter(name, writer) |
||
72 | return this |
||
73 | } |
||
74 | |||
75 | this.getWriter = function () { return context.getWriter(name) } |
||
76 | |||
77 | this.setLevel = function (level) { |
||
78 | context.setLevel(name, level) |
||
79 | return this |
||
80 | } |
||
81 | |||
82 | this.getLevel = function () { return context.getLevel(name) } |
||
83 | |||
84 | /** |
||
85 | * @deprecated |
||
86 | */ |
||
87 | this.setThreshold = this.setLevel |
||
88 | |||
89 | /** |
||
90 | * @deprecated |
||
91 | */ |
||
92 | this.getThreshold = this.getLevel |
||
93 | |||
94 | this.getContext = function () { return context } |
||
95 | |||
96 | this.attach = function (name, value) { mdc[name] = value } |
||
97 | |||
98 | this.detach = function (name) { delete mdc[name] } |
||
99 | |||
100 | this.attachAll = function (values) { mdc = values } |
||
101 | |||
102 | this.detachAll = function () { |
||
103 | var b = mdc |
||
104 | mdc = {} |
||
105 | return b |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @function Slf4j#trace |
||
110 | * @param {string} pattern Logging pattern with `{}` as a place for substitution |
||
111 | * @param {...object} parameters List of pattern parameters |
||
112 | */ |
||
113 | |||
114 | /** |
||
115 | * @function Slf4j#debug |
||
116 | * @param {string} pattern Logging pattern with `{}` as a place for substitution |
||
117 | * @param {...object} parameters List of pattern parameters |
||
118 | */ |
||
119 | |||
120 | /** |
||
121 | * @function Slf4j#notice |
||
122 | * @param {string} pattern Logging pattern with `{}` as a place for substitution |
||
123 | * @param {...object} parameters List of pattern parameters |
||
124 | */ |
||
125 | |||
126 | /** |
||
127 | * @function Slf4j#info |
||
128 | * @param {string} pattern Logging pattern with `{}` as a place for substitution |
||
129 | * @param {...object} parameters List of pattern parameters |
||
130 | */ |
||
131 | |||
132 | /** |
||
133 | * @function Slf4j#warn |
||
134 | * @param {string} pattern Logging pattern with `{}` as a place for substitution |
||
135 | * @param {...object} parameters List of pattern parameters |
||
136 | */ |
||
137 | |||
138 | /** |
||
139 | * @function Slf4j#error |
||
140 | * @param {string} pattern Logging pattern with `{}` as a place for substitution |
||
141 | * @param {...object} parameters List of pattern parameters |
||
142 | */ |
||
143 | } |
||
144 | |||
256 |